import axios from 'axios'
import qs from 'qs'
import host from '@/settings.js'

const service = axios.create({
    baseURL: host.host,
    timeout: 5000
})



service.interceptors.request.use(
    config => {
        return config
    },
    error => {
        console.log(error);
        return Promise.reject(error);
    }
)



service.interceptors.response.use(
    response => {
        const res = response.data
        return res;

    },
    error => {
        console.log('err' + error) // for debug

        return Promise.reject(error)
    }
)


function http(config) {
    console.log('config', config)
    if (config.method.toLowerCase() == 'get') {
        config.params = config.kv
    } else if (config.method.toLowerCase() == 'post') {
        config.headers = {}
        config.headers['Content-Type'] = 'application/x-www-form-urlencoded'
        config.data = qs.stringify(config.kv, { allowDots: true, arrayFormat: 'repeat' })
    }


    return service(config)
}

export default http